<?php

namespace common\behavior\CommonMessagePushing;


use common\model\OrderModel;
use common\model\UserModel;
use common\service\BehaviorService;
use common\service\OrderService;
use Webman\Stomp\Client;

/**
 * 订单审核结果 消息通知
 */
class OrderReviewResultBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'order_id' => 'intval',
                'is_pass' => 'intval',
            ],
            [
                'order_id' => 0,
                'is_pass' => 0,
            ],
            'data',
            ['type' => 'order_review_result'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($orderId, $isPass) = $params;
        if (($isPass === 0) || empty($orderId)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $order = model(OrderModel::class)->find($orderId);
        $toTenantData = teamones_request('teamones-im', 'tenant/find', [
            'param' => [
                'filter' => [
                    'id' => $order['to_tenant_id']
                ]
            ]
        ])['data'];
        $notifyParam = [
            'first' => '发包审批已完成',
            'order_name' => $order['name'],
            'supplier_name' => $toTenantData['name'],
            'review_at' => date("Y/m/d H:i"),
            'review_result' => $isPass ? "通过" : "未通过",
            'remark' => $isPass ? '等待供应商接受' : "请重新发包",
        ];
        $creator = model(UserModel::class)->find($order['created_by']);
        $messageTemplateId = OrderService::getInstance()->getNotifyOrderReviewResultTemplateId();

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $notifyParam,
                'receiver' => $creator['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'message_key' => 'order_create_or_update_review_result_notify'
            ]],
        ]));
        /***********  结束  ************/
    }
}
